package tanke;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JPanel;


class MyPanel extends JPanel implements KeyListener,Runnable{

	Image image1 = null;
	Image image2 = null;
	Image image3 = null;
	Hero hero = null;  	//定义我的坦克
	Vector<Enemy> enemys = new Vector<Enemy>();
	Vector<Bomb> bombs = new Vector<Bomb>();
	Bomb bomb = null;
	int enemySize = 5;		//敌人坦克的数量
	int x = 10;
	int y = 10;
	int direct = 1;			//方向 有1、2、3、4
	int type = 1;			//坦克类型  0我军 亮灰色    1敌军黄色坦克
	int speed = 5;

	public MyPanel(){
		hero = new Hero(x+100,y+150, direct, type, speed);
		for(int i=0;i<enemySize;i++){
			Enemy enemy = new Enemy((x+i*50), y, 2, 0, speed);
			enemys.add(enemy);
			Thread t = new Thread(enemy);
			t.start();
			Shot enemyShot = new Shot(enemy.getX()+10, enemy.getY()+30, enemy.getDirect());
			enemy.enemyShots.add(enemyShot);
			Thread t2 = new Thread(enemyShot);
			t2.start();


		}

		image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
		image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
		image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
	}

	/**
	 * 绘制JPanel面板
	 */
	public void paint(Graphics g){
		super.paint(g);
		g.drawImage(image1, 100, 100, 30, 30, this);
		g.drawImage(image2, 100, 100, 30, 30, this);
		g.drawImage(image3, 100, 100, 30, 30, this);
		g.fillRect(0, 0, 400, 300);
		//绘制自己的坦克
		if(hero.isLive()){
			this.makeTank(hero.getX(), hero.getY(), g, hero.getDirect(), hero.getType());
		}
		//绘制敌人坦克
		for(int i=0,len=enemys.size();i<len;i++){
			Enemy enemy = enemys.get(i);
			if(enemy.isLive()){
				this.makeTank(enemy.getX(), enemy.getY(), g, enemy.getDirect(), enemy.getType());
				//绘制敌人的子弹
				for(int j=0;j<enemy.enemyShots.size();j++){
					Shot eShot = enemy.enemyShots.get(j);
					if(eShot.isLive()){
						g.draw3DRect(eShot.getX(), eShot.getY(),1,1, false);
					}else{
						enemy.enemyShots.remove(eShot);
					}
				}
			}
		}
		//绘制子弹
		for(int i=0;i<hero.shots.size();i++){
			Shot myshot = hero.shots.get(i);
			if(myshot != null && myshot.isLive()){
				this.makeShot(myshot.getX(),myshot.getY() , g, myshot.getDirect(),1);
			}
			if(myshot.isLive() == false){
				hero.shots.remove(myshot);
			}
		}


		for(int i=0;i<bombs.size();i++){
			Bomb b = bombs.get(i);
			System.out.println("bombs.size()= "+bombs.size()+"  b.life="+b.getLife());
			if(b.getLife()>6){
				g.drawImage(image1, b.getX(), b.getY(),30,30, this);
			}else if(b.getLife()>3){
				g.drawImage(image2, b.getX(), b.getY(),30,30, this);
			}else if(b.getLife()>0){
				g.drawImage(image3, b.getX(), b.getY(),30,30, this);
			}
			b.lifeDown();
			if(b.getLife()==0){
				bombs.remove(b);
			}
		}


	}

	/**
	 * 绘制坦克的方法
	 * @param x2
	 * @param y2
	 * @param g
	 * @param direct
	 * @param type
	 */
	public void makeTank(int x2,int y2,Graphics g,int direct,int type){

		switch(type){
			case 0:
				g.setColor(Color.CYAN);			//敌人的坦克   cyan色的
				break;
			case 1:
				g.setColor(Color.YELLOW);
				break;
		}
		switch(direct){
			case 1:             //向上走向的坦克                       绘制出来的图  占内存较少
				g.fill3DRect(x2, y2, 5, 30, false);
				g.fill3DRect(x2+15, y2, 5, 30, false);
				g.fill3DRect(x2+5, y2+5, 10, 20, false);
				g.fillOval(x2+4, y2+8, 10, 10);
				g.drawLine(x2+9, y2, x2+10, y2+15);
				break;
			case 2:            //向下走向的坦克
				g.fill3DRect(x2, y2, 5, 30, false);
				g.fill3DRect(x2+15, y2, 5, 30, false);
				g.fill3DRect(x2+5, y2+5, 10, 20, false);
				g.fillOval(x2+4, y2+10, 10, 10);
				g.drawLine(x2+9, y2+30, x2+9, y2+15);
				break;
			case 3:            //向左走向的坦克
				g.fill3DRect(x2, y2, 30, 5, false);
				g.fill3DRect(x2, y2+15, 30, 5, false);
				g.fill3DRect(x2+5, y2+5, 20, 10, false);
				g.fillOval(x2+8, y2+4, 10, 10);
				g.drawLine(x2, y2+10, x2+15, y2+10);
				break;
			case 4:            //向右走向的坦克
				g.fill3DRect(x2, y2, 30, 5, false);
				g.fill3DRect(x2, y2+15, 30, 5, false);
				g.fill3DRect(x2+5, y2+5, 20, 10, false);
				g.fillOval(x2+9, y2+4, 10, 10);
				g.drawLine(x2+30, y2+10, x2+15, y2+10);
				break;

		}
	}

	/**
	 * 绘制子弹
	 *
	 */
	public void makeShot(int x2,int y2,Graphics g,int direct,int type){

		switch(type){
			case 0:
				g.setColor(Color.CYAN);
				break;
			case 1:
				g.setColor(Color.YELLOW);
				break;
		}
		switch(direct){
			case 1:
			case 2:
			case 3:
			case 4:
				g.draw3DRect(x2, y2, 1, 1, false);
				break;
		}
	}

	/**
	 * 子弹打中敌人的方法
	 *
	 */
	public void shotEnemy(Shot s,Tank e){


		switch(e.getDirect()){
			case 1:
			case 2:
				if(s.getX()>e.getX()&&s.getX()<(e.getX()+20)&&s.getY()>e.getY()&&s.getY()<(e.getY()+30)){
					s.setLive(false);
					e.setLive(false);
					bomb = new Bomb(e.getX(), e.getY());
					bombs.add(bomb);

				}
				break;
			case 3:
			case 4:
				if(s.getX()>e.getX()&&s.getX()<(e.getX()+30)&&s.getY()>e.getY()&&s.getY()<(e.getY()+20)){
					s.setLive(false);
					e.setLive(false);
					bomb = new Bomb(e.getX(), e.getY());
					bombs.add(bomb);
				}
				break;
		}
	}

	//被敌人的坦克击中
	public void shotMe(){

		for(Enemy t:enemys){
			for(Shot shot:t.enemyShots){
				if(hero.isLive()){
					this.shotEnemy(shot, hero);
				}
			}
		}
	}

	/**
	 * 子弹打中敌人
	 */
	public void shotEnemy(){
		for(Shot myshot:hero.shots){
			if(myshot.isLive()){
				for(Enemy enemy:enemys){
					if(enemy.isLive()){
						this.shotEnemy(myshot, enemy);
					}
				}
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_DOWN){
			if(this.hero.getY()<270){
				this.hero.moveDown();
			}
		}else if(e.getKeyCode()==KeyEvent.VK_UP){
			if(this.hero.getY()>0){
				this.hero.moveUp();
			}
		}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
			if(this.hero.getX()>0){
				this.hero.moveLeft();
			}
		}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			if(this.hero.getX()<370){
				this.hero.moveRight();
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_J){
			System.out.println("射击");
			if(hero.shots.size()<5){
				this.hero.shot(this.hero.getX(), this.hero.getY(), this.hero.getDirect());
			}
		}

		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub

		while(true){
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			this.shotEnemy();
			this.shotMe();

			this.repaint();
		}
	}
}
